package com.groot.medical.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;
import com.groot.medical.entity.Menu;
import com.groot.medical.entity.PageBean;
import com.groot.medical.entity.Ztree;
import com.groot.medical.service.MenuService;
import com.groot.medical.service.MenuServiceImpl;
@WebServlet("/sys/menu.do")
public class MenuController extends BaseServletController {
	private MenuService menuService = new MenuServiceImpl();
	
	private static final long serialVersionUID = 1L;
	/*private UserService userService = new UserServiceImpl();*/
	
	
	private static Logger LOGGER = Logger.getLogger(LoginController.class);
	
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}
	/**
	 * 打开权限添加页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openMenu (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	return "/WEB-INF/page/menu/menu_add.jsp";
	}
	
	/**
	 * 添加权限
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		String pageNum = request.getParameter("pageNum");
		System.out.println("tgId_+_+"+tgId);
		
		String menuname = request.getParameter("menuName");
		String menupid = request.getParameter("menuPid");
		String Url = request.getParameter("menuUrl");
		Menu menu = new Menu();
		
		menu.setMenuName(menuname);
		menu.setMenuPid(menupid);
		menu.setMenuUrl(Url);
		
		int i =  menuService.addMenu(menu);
		System.out.println("hahah:::"+i);
		if(i==1){
			/*request.setAttribute("tgId",tgId);*/
			return findMenuByPage(request,response);
			
		}else
			return findAllMenuList(request,response);
		
}	
	/**
	 * 查看权限树（打开权限添加页面）
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllMenuList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		String pageNum = request.getParameter("pageNum");
		List<Menu> menuList = new ArrayList<>();
		
		menuList=menuService.findAllMenuLsit();
		List<Ztree> ztrees = new ArrayList<>();
		for (int i = 0; i < menuList.size(); i++) {
			Ztree ztree = new Ztree();
			Menu menu = menuList.get(i);
			ztree.setId(menu.getMenuId());
			ztree.setpId(menu.getMenuPid());
			ztree.setName(menu.getMenuName());
			ztrees.add(ztree);
		}
		Gson gson = new Gson();
		String json = gson.toJson(ztrees);
		request.setAttribute("menuList", json);
		request.setAttribute("tgId", tgId);
		/*request.setAttribute("pageNum", pageNum);*/
		    
		return "/WEB-INF/page/menu/menu_add.jsp";
	}
	/**
	 * 分页查询权限列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findMenuByPage (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		int pageNum = Integer.valueOf(request.getParameter("pageNum")); 
		List<Menu> menus=(List<Menu>) request.getSession().getAttribute("menuList");
		List<Menu> result = new ArrayList<Menu>();
		/*根据页面传过来的id与该用户所有权限的上一级id比较
		 * 若该权限的上一级传过来的id相同则存入新集合
		 */
		for(Menu menu: menus){
			if(menu.getMenuPid().equals(tgId)){
				result.add(menu);
			}
		}
		Gson gson = new Gson();
		request.setAttribute("resultMenus", gson.toJson(result));
		request.setAttribute("tgId", tgId);
		List<Menu> menuss=menuService.findAllMenuLsit();
		int pageSize = 5;
		PageBean menuPage = menuService.findByPage(pageNum, pageSize);
		
		System.out.println("menuPage:"+menuPage);
		request.setAttribute("menuPage", menuPage);
		request.setAttribute("menuss", menuss);
		return "/WEB-INF/page/menu/menu_list.jsp";
	}
	/**
	 * 打开权限修改页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openMenuEditPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		String menuid = request.getParameter("menuid");
		Menu menu = menuService.findMenuByMenuId(menuid);
		request.setAttribute("tgId", tgId);
		request.setAttribute("menu", menu);
		return "/WEB-INF/page/menu/menu_edit.jsp";
		
	}
	
	/**
	 * 修改权限信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String menuEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		String menuid = request.getParameter("menuid");
		String menupid = request.getParameter("menupid");
		String menuname = request.getParameter("menuname");
		String menuurl = request.getParameter("menuurl");
		System.out.println(menuid);
		System.out.println(menupid);
		System.out.println(menuname);
		System.out.println(menuurl);
		Menu menu = new Menu();
		menu.setMenuId(menuid);
		menu.setMenuPid(menupid);
		menu.setMenuName(menuname);
		menu.setMenuUrl(menuurl);
		menuService.editMenu(menu);
		request.setAttribute("tgId", tgId);
		return findMenuByPage(request,response);
	}
	
	
}